package com.gym.openapi.web.controller;

import com.gym.openapi.common.Const;
import com.gym.openapi.common.ResponseResult;
import com.gym.openapi.common.ResultTable;
import com.gym.openapi.entity.AdminUser;
import com.gym.openapi.entity.Menu;
import com.gym.openapi.services.AdminUserServices;
import com.gym.openapi.services.MenuServices;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ming
 * 2021/11/23
 * description:
 * @RestController//类中所有方法返回json
 * @RequestMapping来映射请求，也就是通过它来指定控制器可以处理哪些URL请求，相当于Servlet中在web.xml中配置
 */
@RestController
@RequestMapping("/user")
public class AdminUserController {
    /**
     *@author:ming
     *@Date: 2021/11/23
     *@description:@Autowired注释添加到需要该类型数组的字段或方法，
     * 则spring会从ApplicationContext中搜寻符合指定类型的所有bean
     */
    @Autowired
    private AdminUserServices userServices;
    @Autowired
    private MenuServices menuServices;

    /**
     *@author:ming
     *@Date: 2021/11/25
     *@description:用户管理界面显示 邮箱 权限 状态查询显示
     */
    @RequestMapping("/table")
    public ResultTable table(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit,
                             String realName,
                             String email,
                             Integer status) {
//        System.out.println("这是realname"+realName);
        ResultTable r=userServices.table(page, limit, realName, email, status);
//        System.out.println("这是r========="+r);
        return r;
    }

    @RequestMapping("/menu_tree")
    public List<Menu> getMenuTree() {
        return menuServices.getAllTree();
    }
    /**
     *@author:ming
     *@Date: 2021/11/25
     *@description:根据中间表查找用户id，全部信息
     */
    @RequestMapping("/user_role")
    public List<Integer> getUserRole(Integer userId) {
        return userServices.getUserRole(userId);
    }

    /**
     *@author:ming
     *@Date: 2021/11/25
     *@description:类似授权信息
     * 删除role user 中间的所有 userid的数据
     * 进行批量新增
     */
    @RequestMapping("/assign_role")
    public ResponseResult assignRole(Integer userId, Integer[] roleIds) {
        try {
            return userServices.assignRole(userId, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("批量操作失败");
    }
    /**
     *@author:ming
     *@Date: 2021/11/25
     *@description:删除单个id信息
     */
    @RequestMapping("/del")
    public ResponseResult del(Integer[] ids) {

        try {
            return userServices.delUsers(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("删除单个失败");
    }


    /**
     *@author:ming
     *@Date: 2021/11/25
     *@description:修改信息
     */
    @RequestMapping("/update")
    public ResponseResult updateUser(AdminUser user) {
        try {
            return userServices.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("修改失败");
    }

    /**
     *@author:ming
     *@Date: 2021/11/25
     *@description:添加信息
     */
    @RequestMapping("/add")
    public ResponseResult addUser(AdminUser user) {
        try {
            return userServices.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("新增失败");
    }


    /**
     * @author:ming
     * @Date: 2021/11/23
     * @description:登录业务
     * session.setAttribute
     * ResponseResult操作结果展示
     */
    @RequestMapping("/login")
    public ResponseResult login(String email, String password, HttpSession session) {
        AdminUser user = userServices.doLogin(email, password);
        if (user!=null) {
            session.setAttribute(Const.IS_LOGIN,user);
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("用户名或密码错误");
    }

    /**
     * @author:ming
     * @Date: 2021/11/23
     * @description:注销登录 @SneakyThrows注解处理java的异常
     * session.invalidate()是将session设置为失效，一般在退出时使用，
     * 但要注意的是：session失效的同时 浏览器会立即创建一个新的session的，
     * 你第一个session已经失效了 所以调用它的getAttribute方法时候一定会抛出NullPointerException的
     */
    @SneakyThrows(Exception.class)
    @RequestMapping("/logout")
    public void logout(HttpSession session, HttpServletResponse response) {
        session.invalidate();
        //重定向
        response.sendRedirect("/login.html");
    }




}
